import CodoBase from "../../../lib/CodoBase";
import CodoError from "../../../lib/CodoError";
import { Wechat } from "../wechat";

export default class CodoWechatGzhUser extends CodoBase {
	/**
	 * 获取公众号的关注者列表 ，openId
	 * - https://developers.weixin.qq.com/doc/offiaccount/User_Management/Getting_a_User_List.html
	 */
	async getFollowersList(next_openid?: string, access_token?: string) {
		const token = access_token || this.app.codo.wechat.gzh.accessToken.getCurrentContextCacheAccessToken();
		const url = `https://api.weixin.qq.com/cgi-bin/user/get?access_token=${token}&next_openid=${next_openid || ""}`;
		const res = await this.app.curl<Wechat.ICommonRespnose & Wechat.gzh.response.GetFollowerUserList>(url, {
			method: "GET",
			dataType: "json",
			contentType: "json",
		});
		if (res.data.errcode) {
			throw new CodoError(`wechat gzh get followers user list error %s %s`, res.data.errcode, res.data.errmsg);
		}
		return res.data;
	}

	/**
	 * 通过公众号openId获取用户基本信息
	 * - https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId
	 */
	async getUserBaseInfo(openId: string, access_token?: string) {
		const token = access_token || this.app.codo.wechat.gzh.accessToken.getCurrentContextCacheAccessToken();
		const url = `https://api.weixin.qq.com/cgi-bin/user/info?access_token=${token}&openid=${openId}&lang=zh_CN`;
		const res = await this.app.curl<Wechat.ICommonRespnose & Wechat.gzh.response.GetUserInfoByOpenId>(url, {
			method: "GET",
			dataType: "json",
			contentType: "json",
		});
		if (res.data.errcode) {
			throw new CodoError(`wechat gzh get one user info by openId error: %s %s`, res.data.errcode, res.data.errmsg);
		}
		return res.data;
	}

	getUserBaseInfoBatch(
		openIds: string[],
		access_token?: string
	): Promise<
		Wechat.ICommonRespnose & {
			user_info_list: Wechat.gzh.response.GetUserInfoByOpenId[];
		}
	>;
	getUserBaseInfoBatch(
		openIds: string[],
		lang?: "zh_CN" | "zh_TW" | "en",
		access_token?: string
	): Promise<
		Wechat.ICommonRespnose & {
			user_info_list: Wechat.gzh.response.GetUserInfoByOpenId[];
		}
	>;
	/**
	 * 批量获取用户信息，多个openId获取多个用户信息
	 * - https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId
	 */
	async getUserBaseInfoBatch(openIds: string[], lang?: string, access_token?: string) {
		if (access_token === undefined && !!lang) {
			access_token = lang;
			lang = "zh_CN";
		} else if (!access_token && !lang) {
			lang = "zh_CN";
		}
		const token = access_token || this.app.codo.wechat.gzh.accessToken.getCurrentContextCacheAccessToken();
		const url = `https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=${token}`;
		const user_list = openIds.map((openId) => ({ openid: openId, lang: lang }));
		const res = await this.app.curl<Wechat.ICommonRespnose & { user_info_list: Wechat.gzh.response.GetUserInfoByOpenId[] }>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				user_list,
			},
		});

		if (res.data.errcode) {
			throw new CodoError(`wechat gzh get one user info batch by openId error: %s %s`, res.data.errcode, res.data.errmsg);
		}
		return res.data;
	}

	/**
	 * 创建标签
	 *
	 * @param {string} name 标签名，长度30个字符内
	 * @param {string} [access_token]
	 * @return {*}
	 * @memberof CodoWechatGzhUser
	 */
	async createTag(name: string, access_token?: string) {
		const token = access_token || this.app.codo.wechat.gzh.accessToken.getCurrentContextCacheAccessToken();
		const url = `https://api.weixin.qq.com/cgi-bin/tags/create?access_token=${token}`;

		const res = await this.app.curl<Wechat.ICommonRespnose | { tag: { id: number; name: string } }>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				tag: {
					name,
				},
			},
		});

		if ("errcode" in res.data && res.data.errcode !== 0) {
			throw new CodoError(`wechat gzh create user tag error: %s %s`, res.data.errcode, res.data.errmsg);
		}
		return res.data;
	}

	/**
	 * 获取已有的标签列表
	 *
	 * @param {string} [access_token]
	 * @return {*} 
	 * @memberof CodoWechatGzhUser
	 */
	async getTagsList(access_token?: string) {
		const token = access_token || this.app.codo.wechat.gzh.accessToken.getCurrentContextCacheAccessToken();
		const url = `https://api.weixin.qq.com/cgi-bin/tags/get?access_token=${token}`;

		const res = await this.app.curl<Wechat.ICommonRespnose | { tags: { id: number; name: string; count: number }[] }>(url, {
			method: "GET",
			dataType: "json",
			contentType: "json",
			data: {},
		});

		if ("errcode" in res.data && res.data.errcode !== 0) {
			throw new CodoError(`wechat gzh get user tag list error: %s %s`, res.data.errcode, res.data.errmsg);
		}
		return res.data;
	}
}
